package com.java.onlineshop.impl;

import com.java.onlineshop.dao.IMyOrderDao;
import com.java.onlineshop.pojo.MyOrder;
import com.java.onlineshop.pojo.OrderItem;
import com.java.onlineshop.util.JdbcUtil;

import java.sql.*;
import java.util.Iterator;
import java.util.Set;

/**
 * @program: onlineshop
 * @description: MyOrderDaoImpl
 * @author: ShuangChenYue
 * @create: 2021-06-16 20:24
 **/
public class MyOrderDaoImpl implements IMyOrderDao
{

    @Override
    public void addMyOrder(MyOrder myOrder)
    {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try
        {
            //1.获取连接
            connection = JdbcUtil.getConnection();
            //2.设置手动提交
            connection.setAutoCommit(false);
            //3.预编译sql语句
            String sql = "insert into myorder(name,cost,userid,paywayid,orderstatusid) values(?,?,?,?,?)";
            preparedStatement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);//在预编译时顺便生成一个主键
            //4.填充占位符
            preparedStatement.setString(1, myOrder.getName());
            preparedStatement.setDouble(2,myOrder.getCost());
            preparedStatement.setInt(3,myOrder.getUser().getId());
            preparedStatement.setInt(4,myOrder.getPayWay().getPaywayid());
            preparedStatement.setInt(5,myOrder.getOrderStatus().getOrderstatusid());
            //5.执行操作
            int i = preparedStatement.executeUpdate();
            System.out.println(i+"条记录被影响");
            //6.处理主键
            int orderid = 0;//先初始化orderid
            resultSet = preparedStatement.getGeneratedKeys();//此处返回一个结果集，该结果集中存放了一个主键
            //7.若主键中有东西，则将主键赋值给orderid
            if (resultSet.next())
            {
                orderid = resultSet.getInt(1);
            }
            //8.将当前ps关闭，以便于ps二次执行
            JdbcUtil.relase(preparedStatement);
            //9.给orderitem表插入数据
            //预编译sql语句
            String linesql = "insert into orderitem(amount,productid,myorderid) values(?,?,?)";
            preparedStatement = connection.prepareStatement(linesql);
            //10.从myorder中得到集合，再遍历该集合，顺便填充占位符
            Set<OrderItem> orderItemsList = myOrder.getOrderItems();
            Iterator<OrderItem> iterator = orderItemsList.iterator();
            while (iterator.hasNext())
            {
                OrderItem orderItem = iterator.next();
                //逐个填充占位符
                preparedStatement.setInt(1,orderItem.getAmount());
                preparedStatement.setInt(2,orderItem.getProduct().getId());
                preparedStatement.setInt(3,orderItem.getMyOrder().getMyorderid());
                //11.执行操作
                int j = preparedStatement.executeUpdate();
                System.out.println(j+"条记录被影响");
            }
            //12.手动提交
            connection.commit();
        }catch (Exception e)
        {
            //若发生错误，则数据回滚
            try
            {
                connection.rollback();
            } catch (SQLException throwables)
            {
                throwables.printStackTrace();
            }
            e.printStackTrace();
        }finally
        {
            JdbcUtil.relase(resultSet,preparedStatement,connection);//关闭
        }

    }
}
